package com.yiyahui.project.erp.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.yiyahui.common.utils.poi.ExcelUtil;
import com.yiyahui.framework.aspectj.lang.annotation.Log;
import com.yiyahui.framework.aspectj.lang.enums.BusinessType;
import com.yiyahui.framework.web.controller.BaseController;
import com.yiyahui.framework.web.domain.AjaxResult;
import com.yiyahui.framework.web.page.TableDataInfo;
import com.yiyahui.project.erp.domain.YiyaClinicPerson;
import com.yiyahui.project.erp.domain.YiyaVisitTc;
import com.yiyahui.project.erp.domain.requestVo.YiyaVisitTcRequest;
import com.yiyahui.project.erp.domain.responseVo.VisitTcExtend;
import com.yiyahui.project.erp.service.IYiyaClinicPersonService;
import com.yiyahui.project.erp.service.IYiyaVisitTcService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * tc回访Controller
 * 
 * @author yiyahui
 * @date 2020-05-08
 */
@Api("tc回访")
@RestController
@RequestMapping("/erp/tc")
public class YiyaVisitTcController extends BaseController
{
    @Autowired
    private IYiyaVisitTcService yiyaVisitTcService;

    @Autowired
    private IYiyaClinicPersonService yiyaClinicPersonService;
 
    /**
     * 查询tc回访列表
     */
    @ApiOperation("查询tc回访列表")
    @PreAuthorize("@ss.hasPermi('erp:tc:list')")
    @GetMapping("/list")
    public TableDataInfo list(YiyaVisitTcRequest yiyaVisitTcRequest)
    {
        startPage();
        List<YiyaVisitTc> list = yiyaVisitTcService.selectYiyaVisitTcList(yiyaVisitTcRequest);
        return getDataTable(list);
    }

    /**
     * 导出tc回访列表
     */
    @ApiOperation("导出tc回访列表")
    @PreAuthorize("@ss.hasPermi('erp:tc:export')")
    @Log(title = "tc回访", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(YiyaVisitTcRequest yiyaVisitTcRequest)
    {
        List<YiyaVisitTc> list = yiyaVisitTcService.selectYiyaVisitTcList(yiyaVisitTcRequest);
        ExcelUtil<YiyaVisitTc> util = new ExcelUtil<YiyaVisitTc>(YiyaVisitTc.class);
        return util.exportExcel(list, "tc");
    }

    /**
     * 获取tc回访详细信息
     */
    @ApiOperation("获取tc回访详细信息")
    @ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "Long", paramType = "path")
    @PreAuthorize("@ss.hasPermi('erp:tc:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yiyaVisitTcService.selectYiyaVisitTcById(id));
    }

    /**
     * 新增tc回访
     */
    @ApiOperation("新增tc回访")
    @ApiImplicitParam(name = "yiyaVisitTc", value = "${tableComment}", dataType = "YiyaVisitTc")
    @PreAuthorize("@ss.hasPermi('erp:tc:add')")
    @Log(title = "tc回访", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YiyaVisitTc yiyaVisitTc)
    {
        return toAjax(yiyaVisitTcService.insertYiyaVisitTc(yiyaVisitTc));
    }

    /**
     * 修改tc回访
     */
    @ApiOperation("修改tc回访")
    @ApiImplicitParam(name = "yiyaVisitTc", value = "${tableComment}", dataType = "YiyaVisitTc")
    @PreAuthorize("@ss.hasPermi('erp:tc:edit')")
    @Log(title = "tc回访", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YiyaVisitTc yiyaVisitTc)
    {
        return toAjax(yiyaVisitTcService.updateYiyaVisitTc(yiyaVisitTc));
    }

    /**
     * 删除tc回访
     */
    @ApiOperation("删除tc回访")
    @ApiImplicitParam(name = "ids", value = "主键id数组", required = true, dataType = "Long[]", paramType = "path")
    @PreAuthorize("@ss.hasPermi('erp:tc:remove')")
    @Log(title = "tc回访", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yiyaVisitTcService.deleteYiyaVisitTcByIds(ids));
    }

    /**
     * 自定义tc回访列表
     */
    @ApiOperation("自定义tc回访列表")
    @GetMapping("/extend/list")
    public TableDataInfo extendList(YiyaVisitTcRequest yiyaVisitTcRequest)
    {
        startPage();
        List<YiyaVisitTc> list = yiyaVisitTcService.selectYiyaVisitTcList(yiyaVisitTcRequest);
        Map<Long, YiyaClinicPerson> yiyaClinicPersonMap = yiyaClinicPersonService.selectByIds(list.stream().map(YiyaVisitTc::getClinicPersonId).collect(Collectors.toList())).stream().collect(Collectors.toMap(YiyaClinicPerson::getId, a -> a, (k1, k2) -> k1));
        List<VisitTcExtend> visitTcExtends = new ArrayList<>();
        list.forEach(r -> {
            VisitTcExtend visitTcExtend = new VisitTcExtend();
            visitTcExtend.setYiyaVisitTc(r);
            visitTcExtend.setYiyaClinicPerson(yiyaClinicPersonMap.get(r.getClinicPersonId()));
            visitTcExtends.add(visitTcExtend);
        });
        return getDataTable(visitTcExtends);
    }
}
